package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-21
 * Time: 16:51
 */
public class Demo7 {
    public static void main(String[] args) throws InterruptedException {
        /**
         * 前台线程结束 整个进程才会结束
         * 后台线程的结束不影响进程，但进程结束了 后台线程一定随之结束
         * 创建的线程和main线程都是默认前台线程需要setDaemon来改变
         */
        Thread thread = new Thread(() -> {
            while (true) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //设置为后台线程
        //设置在线程开始之前
        thread.setDaemon(true);//设置为后台线程
        thread.start();

        for (int i = 0; i < 3; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        System.out.println("main 结束");
        //main前台线程，后台线程thread也会结束
    }
}
